let app = getApp();
var httptool = require("../../utils/zry.httptool.js");
let bluetoothtool = require("../../utils/bluetoothtool.js");
Page({
  data:{
    curAchievement:{},//当前回合成绩
    achievements:[],//成绩列表
    bgCover:true,//遮罩层隐藏
    shareArea:true,//分享选择区域
    rounds:'',//回合数
    years:'',//哪一年
    BottomId:'',//局ID
    BoxRecordId:'',//回合ID
    ID:'',//打擂的成绩ID
    shareType:-1,//强制分享的类型
    againcover:false, 
    shareBtns:true,//强制分享button
    msgprop:'',//消息提示  
    imgurl: 'http://mpic.tiankong.com/382/8a3/3828a395a0283ce2f899257e638a9188/640.jpg',
    title:"分享给微信朋友"
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    let that=this;
    let shareType=options.shareType;
    if (!shareType){
      shareType = 0;
    }
    
    that.setData({ shareType: shareType, shareBtns: shareType!=1 });
    that.getShareImg();
    that.getAchievements();
  },
  onReady:function(){
    // 页面渲染完成
  },
  cancelPop:function(e){
    let that=this;
    that.setData({
      bgCover:!that.data.bgCover,
      shareArea:!that.data.shareArea
    });    
  },
  getShareImg: function () {
    let that = this;
    httptool.ajaxPost("api/BannerPicture/GetBannerPictureByType", { BannerType: "WxShare" }, function (res) {
      if (res.suc) {
        that.setData({ imgurl: res.data[0].ImgUrl });
      }
    });
  },
  tryAgain:function(){
    let that = this;
    let UserId = app.getStorage('customerId');
    httptool.ajaxPost("api/BoxRecord/IsForceShare", { UserId: UserId }, function (res) {
      if(res.suc){
        if(res.data){
          app.showNotify(that,"请先进行一次微信分享！");
          return false;
        }
        let deviceId = wx.getStorageSync("deviceId");
        let serviceId = wx.getStorageSync("serviceId");
        let uuid = wx.getStorageSync("uuid");
        
        let hex = "404d";//@M
        bluetoothtool.writeBLECharacteristicValue(deviceId, serviceId, uuid, hex,function(){
          wx.setStorageSync("boxStatus", "选择模式");
          wx.redirectTo({
            url: '/pages/contest/contest'
          });
        });
      }else{
         app.showNotify(that, res.msg);
      }
    });
  },
  stop: function () {//清除缓存，发送reset命令给设备
    let deviceId = wx.getStorageSync("deviceId");
    let serviceId = wx.getStorageSync("serviceId");
    let uuid = wx.getStorageSync("uuid");
    wx.removeStorageSync("deviceId");
    wx.removeStorageSync("deviceName");
    wx.removeStorageSync("boxStatus");

    bluetoothtool.writeBLECharacteristicValue(deviceId, serviceId, uuid, "405245534554", function () {
      wx.redirectTo({
        url: '/pages/index/index'
      });
    });    
  },
  getAchievements:function(e){//获取成绩列表
    let that=this;
    let UserId=app.getStorage('customerId');
     httptool.ajaxPost("api/BoxRecord/GetBoxCurrentRecords", {UserId:UserId}, function (res) {
        if (res.suc) {
          let data = app.notNull(res.data, []);
          if(data.length>0){
            for(let i=0;i<data.length;i++){
                let boxNumber=data[i].BoxRecordId;
                let arr1=data[i].BoxDate.split(' ');
                data[i].BoxDate=data[i].BoxDate.substring(5);
                let arr2=arr1[0].split("-");
                data[i].years=arr2[0];
                let boxRecordIds = ["一", "二", "三", "四", "五", "六", "七", "八", "九","十"];
                data[i].BoxRecordName = boxRecordIds[boxNumber-1];                
            }
            that.setData({achievements: data,curAchievement:data[0]});
         
          }
         
        } else {
          app.showNotify(that, res.msg);
        }
      });
  },
  shareScore:function(e){//点击分享按钮
    let that=this;
    let BottomId=e.currentTarget.dataset.bid;
    let BoxRecordId=e.currentTarget.dataset.brid;
    let ID=e.currentTarget.dataset.id;
    if(that.data.shareType==1){
      that.setData({againcover:true,shareBtns:false,BottomId:BottomId,BoxRecordId:BoxRecordId,ID:ID});
    }else{
      that.setData({
        bgCover: false, shareArea: false, shareBtns: true,BottomId:BottomId,BoxRecordId:
      BoxRecordId,ID:ID});
    }
    httptool.ajaxPost("api/BoxRecord/GetBoxRecordShared", { ID: that.data.ID }, function (res) {
      if (res.suc) {
        that.setData({ title: res.data.Title });
      }
      });
  },
  closeBtn:function(e){//分享按钮关闭
    let that=this;
    that.setData({bgCover:true,shareArea:true});
  },
  shareLeiCircle:function(){//分享到擂友圈
    let that=this;
    wx.navigateTo({
      url: '/pages/share/share?shareId='+1+"&BottomId="+that.data.BottomId+
      "&BoxRecordId="+that.data.BoxRecordId+"&ID="+that.data.ID,
    })
  },
  onShareAppMessage:function(res){
    console.log(res);
    let that=this;
    let BottomId = this.data.BottomId;
    if (!BottomId){
      BottomId = res.target.dataset.bid;
    }
    let BoxRecordId = this.data.BoxRecordId;
    if (!BottomId) {
      BottomId = res.target.dataset.brid;
    }
    let ID = this.data.ID;
    if (!BottomId) {
      BottomId = res.target.dataset.id;
    }
    return {
      title: that.data.title,
      desc: '打擂成绩分享',
      path: '/pages/sFrinedCircle/sharefriend?ID=' + ID + "&BottomId=" + BottomId
      + "&BoxRecordId=" + BoxRecordId+"&UserId"+app.getStorage('customerId'),
      imageUrl: that.data.imgurl,
      success: function (res) {
        // 转发成功
        that.setData({bgCover:true,shareArea:true});
        let params={};
        params.UserId=app.getStorage('customerId');
        params.BottomId = BottomId;
        params.SharedRecordId = BoxRecordId;

        httptool.ajaxPost("api/BoxBottom/GetWXShared", params, function (res) {          
          if (res.suc) {
            console.log("分享成功，发送@Next");
            let deviceId = wx.getStorageSync("deviceId");
            let serviceId = wx.getStorageSync("serviceId");
            let uuid = wx.getStorageSync("uuid");
            console.log("deviceId=" + deviceId);
            bluetoothtool.writeBLECharacteristicValue(deviceId, serviceId, uuid, "404E455854",function(){
              if (that.data.shareType > 0) {
                wx.navigateTo({
                  url: '/pages/contest/contest',
                });
              } else {
                wx.navigateTo({
                  url: '/pages/contestscore/contestscore',
                });
              }        
            });//发送@Next                
          } else {
             app.showNotify(that, res.msg);
          }
        });
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})